package com.ibiaoke.boss.dao;

import com.ibiaoke.boss.entity.SysUser;
import com.ibiaoke.boss.entity.SysUserExample;
import com.ibiaoke.boss.mapper.SysUserMapper;
import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.exception.ServiceException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by huhaichao on 2017/6/6.
 */
@Repository
public class SysUserDao {
    @Autowired
    private SysUserMapper sysUserMapper;

    public SysUser getByName(String name){
        SysUserExample example = new SysUserExample();
        example.createCriteria().andUserNameEqualTo(name).andDelStatusEqualTo((byte) 0);
        List<SysUser> sysUsers = sysUserMapper.selectByExample(example);
        return CollectionUtils.isEmpty(sysUsers)?null:sysUsers.get(0);
    }

    public void registerUser(SysUser user) {
        int i = sysUserMapper.insertSelective(user);
        if(i != 1) {
            throw new ServiceException("注册失败");
        }
    }
}
